package com.guanghua.brick.db;

import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceUtils;

import com.guanghua.brick.BrickConstant;
import com.guanghua.brick.util.IConfig;
import com.guanghua.brick.util.SpringBeanUtil;

/**
 * 通过spring配置的ds来获取数据库连接
 * @author leeon
 */
public class SpringSQLConnection implements ISQLConnection {

	public Connection createConnection(String bean) throws Exception {
		return DataSourceUtils.getConnection(createDataSource(bean));
	}

	public DataSource createDataSource(String bean) throws Exception {
		IConfig config = BrickConstant.APP_DB_CONN_CONFIG;
		if (bean == null) {
			bean = config.getText("default-bean");
		}
		
		return (DataSource)SpringBeanUtil.getBeanFactory().getBean(bean);
	}

}
